layui.use(['form', 'jquery'], function () {

    const form = layui.form,
        $ = layui.jquery;

    let id = sessionStorage.getItem("teamLeaderId");
    let teamCode = sessionStorage.getItem("teamCode");
    $("#id").val(id);
    findUserByTeamCode(teamCode);
    findById(id);

    /**
     * 查询单个
     * @param id
     */
    function findById(id) {
        let result = commonAjax('/team/findById', {id: id}, 'get');
        $('#leader').val(result.data.leader);
        form.render();
    }

    /**
     * 保存
     */
    form.on('submit(commit)', function (data) {
        let formData = JSON.stringify(data.field);

        let result = commonAjax('/team/distributeLeader', formData, 'post');
        if (result.code === 200) {
            layerMsgSaveSuccess();
        } else {
            layerMsgSaveFail();
        }

        return false;
    });

    /**
     * 查询该小组所有成员
     */
    function findUserByTeamCode(teamCode) {

        let data = {
            pageNum: 1,
            pageSize: 50,
            teamCode: teamCode
        };

        let result = commonAjax('/user/findUserByTeamCode', data, 'get');
        let html = '<option value="">请选择</option>';
        for (let i = 0; i < result.data.length; i++) {
            html += '<option value="' + result.data[i].code + '">' + result.data[i].username + '</option>';
        }
        $("#leader").html(html);
    }

});